package zpin.action;

import java.util.ArrayList;
import java.util.List;

import util.dao.Pagination;
import zpin.dao.TypeDao;
import zpin.model.Type;

import com.opensymphony.xwork2.ActionSupport;

public class TypeAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	private Type type;
	
	private TypeDao typeDao;
	
	private Long id;
	
	List<Type> typelist = new ArrayList<Type>();
	
	private Pagination<Type> pagination;
	
	public Pagination<Type> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Type> pagination) {
		this.pagination = pagination;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<Type> getTypelist() {
		return typelist;
	}

	public void setTypelist(List<Type> typelist) {
		this.typelist = typelist;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public TypeDao getTypeDao() {
		return typeDao;
	}

	public void setTypeDao(TypeDao typeDao) {
		this.typeDao = typeDao;
	}
	
	public String create(){
		typeDao.create(type);
		return SUCCESS;
	}
	
	public String show(){
		if (pagination == null) {
			this.pagination = new Pagination<Type>();
		}

		if (this.pagination.getSize() == 0) {
			this.pagination.setSize(5);
		}
		if (this.pagination.getNo() == 0) {
			this.pagination.setNo(1);
		}
		
		typeDao.browse(pagination);
		typelist = pagination.getResults();
		pagination.setOrderFieldName("name");
		
		return SUCCESS;
	}
	
	public String delete(){
		typeDao.delete(type);
		return SUCCESS;
	}
	
	public String preupdate(){
		type = typeDao.findById(id);
		return SUCCESS;
	}
	
	public String update(){
		typeDao.update(type);
		
		return SUCCESS;
	}
	
}
